package com.sxc.oa.web.action;


import jakarta.servlet.ServletException;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

public class DepDetailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.print("<!DOCTYPE html>");
        out.print("<html lang='en'>");
        out.print("<head>");
        out.print("<meta charset='UTF-8'>");
        out.print("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
        out.print("<title>部门详情</title>");
        out.print("</head>");
        out.print("<body>");
        out.print("<h1>部门详情</h1>");
        out.print("<h2>=========</h2>");
        out.print("<hr>");

        // 获取部门编号
        String deptno = request.getParameter("deptno");

        // 链接数据库，根据部门编号查询部门信息
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        ResultSet rs2 = null;

        try {
            String username = "root";
            String url = "jdbc:mysql://localhost:3306/dept";
            String password = "123456";
            conn = DriverManager.getConnection(url, username, password);

            // 查询部门信息
            String sql = "SELECT dname, loc FROM dept WHERE deptno = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, deptno);
            rs = ps.executeQuery();

            if (rs.next()) {
                String dname = rs.getString("dname");
                String loc = rs.getString("loc");

                out.print("部门编号：" + deptno + "<br>");
                out.print("部门名称：" + dname + "<br>");
                out.print("部门位置：" + loc + "<br>");
                out.print("<h2>员工信息</h2>");
                out.print("<ul>");

                // 查询员工信息
                sql = "SELECT id, name, zhuanye FROM sss WHERE deptno1 = ?";
                ps = conn.prepareStatement(sql);
                ps.setString(1, deptno);
                rs2 = ps.executeQuery();

                while (rs2.next()) {
                    String id = rs2.getString("id");
                    String name = rs2.getString("name");
                    String zhuanye = rs2.getString("zhuanye");

                    out.print("<li>学号：" + id + "</li>");
                    out.print("<li>姓名：" + name + "</li>");
                    out.print("<li>专业：" + zhuanye + "</li>");

                }
                out.print("</ul>");
            } else {
                out.print("未找到该部门信息");
            }
        } catch (SQLException e) {
            // 处理 SQL 异常
            out.print("发生数据库异常：" + e.getMessage());
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (rs != null) rs.close();
                if (rs2 != null) rs2.close();
                if (ps != null) ps.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                // 处理关闭资源时的异常
                out.print("关闭资源时发生异常：" + e.getMessage());
                e.printStackTrace();
            }
        }

        out.print("<input type='button' value='后退' onclick='window.history.back()'/>");
        out.print("</body>");
        out.print("</html>");
    }
}

/*public class DepDetailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.print("<!DOCTYPE html>");
        out.print("<html lang='en'>");
        out.print("<head>");
        out.print("<meta charset='UTF-8'>");
        out.print("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
        out.print("<title>部门详情</title>");
        out.print("</head>");
        out.print("<body>");
        out.print("<h1>部门详情</h1>");
        out.print("<h2>=========</h2>");
        out.print("<hr>");

        // 获取部门编号
        String deptno = request.getParameter("deptno");

        // 链接数据库，根据部门编号查询部门信息
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            String username = "root";
            String url = "jdbc:mysql://localhost:3306/dept";
            String password = "123456";
            conn = DriverManager.getConnection(url, username, password);

            String deptno = request.getParameter("deptno");
            String sql = "SELECT name, zhuanye, identity FROM acm WHERE deptno = ?";
            if ("20".equals(deptno)) {
                sql = "SELECT name, zhuanye, identity FROM web WHERE deptno = ?";
            }

            ps = conn.prepareStatement(sql);
            ps.setString(1, deptno);
            rs = ps.executeQuery();

            // 处理结果集
            if (rs.next()) {
                out.print("<ul>");
                do {
                    String name = rs.getString("name");
                    String zhuanye = rs.getString("zhuanye");
                    String identity = rs.getString("identity");

                    // 使用 <ul> 列表显示部门信息
                    out.print("<li>姓名：" + name + "</li>");
                    out.print("<li>专业：" + zhuanye + "</li>");
                    out.print("<li>身份：" + identity + "</li>");
                } while (rs.next());
                out.print("</ul>");
            } else {
                out.print("未找到该部门信息");
            }
        } catch (SQLException e) {
            // 处理 SQL 异常
            out.print("发生数据库异常：" + e.getMessage());
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (rs != null) rs.close();
                if (ps != null) ps.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                // 处理关闭资源时的异常
                out.print("关闭资源时发生异常：" + e.getMessage());
                e.printStackTrace();
            }
        }

        out.print("<input type='button' value='后退' onclick='window.history.back()'/>");
        out.print("</body>");
        out.print("</html>");
    }
}*/
  /*  @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.print("<!DOCTYPE html>");
        out.print("<html lang='en'>");
        out.print("<head>");
        out.print("<meta charset='UTF-8'>");
        out.print("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
        out.print("<title>部门详情</title>");
        out.print("</head>");
        out.print("<body>");
        out.print("<h1>部门详情</h1>");
        out.print("<h2>=========</h2>");
        out.print("<hr>");

        // 获取部门编号
        String deptno = request.getParameter("deptno");

        // 链接数据库，根据部门编号查询部门信息
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            String username = "root";
            String url = "jdbc:mysql://localhost:3306/dept";
            String password = "123456";
            conn = DriverManager.getConnection(url, username, password);

            String sql = "SELECT name, zhuanye,identity FROM acm WHERE deptno = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, deptno);
            rs = ps.executeQuery();

            // 处理结果集
            if (rs.next()) {
                String name = rs.getString("name");
                String zhuanye = rs.getString("zhuanye");
                out.print("部门编号：" + deptno + "<br>");
                out.print("部门名称：" + name + "<br>");
                out.print("部门位置：" + zhuanye + "<br>");
            } else {
                out.print("未找到该部门信息");
            }
        } catch (SQLException e) {
            // 处理 SQL 异常
            out.print("发生数据库异常：" + e.getMessage());
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (rs != null) rs.close();
                if (ps != null) ps.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                // 处理关闭资源时的异常
                out.print("关闭资源时发生异常：" + e.getMessage());
                e.printStackTrace();
            }
        }

        out.print("<input type='button' value='后退' onclick='window.history.back()'/>");
        out.print("</body>");
        out.print("</html>");
    }
}
*/

/*
public class DepDetailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
         PrintWriter out=response.getWriter();
         out.print("<!DOCTYPE html>");
         out.print("<html lang='en'>");
         out.print("<head>");
         out.print("<meta charset='UTF-8'>");
         out.print("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
         out.print("<title>部门详情</title>");
         out.print("</head>");
         out.print("<body>");
         out.print("<h1>部门详情</h1>");
         out.print("<h2>=========</h2>");
         out.print("<hr>");

        //获取部门编号
        // /oa/dept/detail？deptno=30
        //虽然提交的是30，但是服务器获取的是30这个字符串
        String deptno = request.getParameter("deptno");
        //链接数据库，根据部门编号查询部门信息
        Connection conn = null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        DButil DButil = null;

        try {

            String username="root";
            String url = "jdbc:mysql://localhost:3306/dept";
            String password="123456";
            conn= DriverManager.getConnection(url, username, password);
            out.print("<h2>=========+++++1</h2>");

            String sql = "select dename,loc from dept where deptno = ?";
            out.print("<h2>=========+++++2</h2>");

            ps=conn.prepareStatement(sql);
            out.print("<h2>=========+++++3</h2>");

            ps.setString(1,deptno);
            out.print("<h2>=========+++++4</h2>");

            rs = ps.executeQuery();
            out.print("<h2>=========+++++6</h2>");

            //这个结果集一定只有一条记录
            if(rs.next()){
                out.print("<h2>=========+++++++3</h2>");
                String dname=rs.getString("dename");
                String loc=rs.getString("loc");
                out.print("部门编号："+deptno+"<br>");
                out.print("部门名称："+dname+"<br>");
                out.print("部门位置："+loc+"<br>");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                com.sxc.oa.utils.DButil.close(conn,ps,rs);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        out.print("<input type='button' value='后退' onclick='window.history.back()'/>");
        out.print("</body>");
        out.print("</html>");

    }
}

 */